#include "RenderTest.h"

#include "RenderDevice.h"
#include "d3dx9.h"

//////////////////////////////////////////////////
//RenderTest
//////////////////////////////////////////////////

struct Vertex
{
	Vertex(){}
	Vertex(float x, float y, float z, DWORD color)
	{
		_x = x;  _y = y;  _z = z;
    _color = color;
	}
	float _x, _y, _z;
  DWORD _color;
};

//------------------------------------------------

RenderTest::RenderTest()
  {
  }

//------------------------------------------------

RenderTest::~RenderTest(void)
  {
  }

//------------------------------------------------

void RenderTest::Render()
  {
  D3DXVECTOR3 position(0.0f, 0.5f, -2.0f);
	D3DXVECTOR3 target(0.0, 0.5f, 0.0f);
	D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
    D3DXMATRIX V;
	D3DXMatrixLookAtLH(&V, &position, &target, &up);

    m_render_device->GetDirect3DDevice()->SetTransform(D3DTS_VIEW, &V);

	D3DXMATRIX proj;
	D3DXMatrixPerspectiveFovLH(
			&proj,
			D3DX_PI * 0.5f, // 90 - degree
			(float)800 / (float)600,
			1.0f,
			1000.0f);
	m_render_device->GetDirect3DDevice()->SetTransform(D3DTS_PROJECTION, &proj);

  m_render_device->GetDirect3DDevice()->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
	
	m_render_device->GetDirect3DDevice()->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
	m_render_device->GetDirect3DDevice()->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
	m_render_device->GetDirect3DDevice()->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
	m_render_device->GetDirect3DDevice()->SetRenderState(D3DRS_ZFUNC, D3DCMP_LESSEQUAL);

	m_render_device->GetDirect3DDevice()->BeginScene();

  m_render_device->GetDirect3DDevice()->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 3, 0, 1);
	
	m_render_device->GetDirect3DDevice()->EndScene();
	m_render_device->GetDirect3DDevice()->Present(0, 0, 0, 0);
  }

//------------------------------------------------

void RenderTest::SetRenderDevice(RenderDevice* i_render_device)
  {
  m_render_device = i_render_device;

  m_render_device->GetDirect3DDevice()->CreateVertexBuffer(
		3 * sizeof(Vertex), 
		D3DUSAGE_WRITEONLY,
		D3DFVF_XYZ | D3DFVF_DIFFUSE,
		D3DPOOL_MANAGED,
		&VB,
		0);

	m_render_device->GetDirect3DDevice()->CreateIndexBuffer(
		3 * sizeof(WORD),
		D3DUSAGE_WRITEONLY,
		D3DFMT_INDEX16,
		D3DPOOL_MANAGED,
		&IB,
		0);

	//
	// Fill the buffers with the cube data.
	//

	// define unique vertices:
	Vertex* vertices;
	VB->Lock(0, 0, (void**)&vertices, 0);

	// vertices of a triangle
	vertices[0] = Vertex(0.0f, 1.0f, 0.0f, D3DCOLOR_XRGB(0, 0, 255));
	vertices[1] = Vertex(1.0f,  0.0f, 0.0f, D3DCOLOR_XRGB(0, 255, 0));
	vertices[2] = Vertex(-1.0f,  0.0f, 0.0f, D3DCOLOR_XRGB(255, 0, 0));

	VB->Unlock();

	// define the triangles of the cube:
	WORD* indices = 0;
	IB->Lock(0, 0, (void**)&indices, 0);

	// front side
	indices[0]  = 0; indices[1]  = 1; indices[2]  = 2;

	IB->Unlock();

	m_render_device->GetDirect3DDevice()->SetStreamSource(0, VB, 0, sizeof(Vertex));
	m_render_device->GetDirect3DDevice()->SetIndices(IB);
	m_render_device->GetDirect3DDevice()->SetFVF(D3DFVF_XYZ | D3DFVF_DIFFUSE);
  m_render_device->GetDirect3DDevice()->SetRenderState(D3DRS_LIGHTING,FALSE);

  }

//------------------------------------------------

//////////////////////////////////////////////////
